#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe
#
# $Id$

from aggmon.databases.metric.metric_database import *


__all__ = ["MetricDatabaseDumper"]


class MetricDatabaseDumper():
    def __init__( self, database_path, group_path, db=None ):
        self.database_path = database_path
        self.group_path = group_path
        if db == None:
            self.db = MetricDatabase( database_path, group_path )
        else:
            self.db = db

    def dump_metric( self, host_name, metric_name ):
        return repr( self.db.getLastMetricByMetricName( host_name, metric_name ) )

    def dump_records( self, host_name, metric_name, start, end, step ):
        return self.db.getRecordsByMetricName( host_name, metric_name, start, end, step )

    def dump( self, host_name, metric_name, start, end, step ):
        print "%s/%s/%s" % ( self.group_path, host_name, metric_name )
        if long( start ) >= long( end ):
            return self.dump_metric( host_name, metric_name )
        else:
            return self.dump_records( host_name, metric_name, long( start ), long( end ), long( step ) )
